/* 
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, or 
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License along 
 * with this program; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef IMAGEMANAGER_H
#define IMAGEMANAGER_H

#include <QWidget>
#include <QThread>
#include <QFuture>
#include <QFutureWatcher>
#include <QTreeWidgetItem>
#include "imagestorage.h"

class MainWindow;

namespace Ui
{
	struct ImageManager;
}

class ImageManager : public QWidget
{
	Q_OBJECT
public:
	ImageManager(MainWindow* mainform, QWidget* widget = 0);
	virtual ~ImageManager();
	void updateList();
	void setMode(bool select);
	QString getSelectedPath();

signals:
	void pictureSelected(const QString& path);

public slots:
	void contextMenu(const QPoint& p);
	void refreshTaglist();
	void tagClicked(QTreeWidgetItem* item, int column);
	void clearQuery();
	void exportImages();

	void imagesLoaded();
	void imageLoadingDone(int index);
	void imageLoadProgress(int progress);

	void prevPage();
	void nextPage();

protected:
	virtual void showEvent(QShowEvent* event);
	virtual void hideEvent(QHideEvent* event);
	virtual void closeEvent(QCloseEvent* event);
	bool eventFilter(QObject* obj, QEvent* event);
	virtual bool event(QEvent* event);
	void updateQueryLineEdit();
	void updatePageLine();

	void doRefreshTaglist();

private:
	Ui::ImageManager* m_ui;
	MainWindow* m_mainform;
	int m_curPos;
	QList<Wishmaster::ImageDescriptor> m_imageList;
	int m_timerID;
	bool m_select;
	QThread* m_loadingThread;
	QList<QImage> m_images;
	QFuture<QImage> m_imageFuture;
	QFutureWatcher<QImage> m_imageFutureWatcher;

	bool m_tagsEmpty;

	int m_itemsInPage;
	int m_currentPage;
	int m_maxPage;

	static const int RolePicPath = Qt::UserRole + 1;
	static const int RoleTag = Qt::UserRole + 2;
	static const int RoleId = Qt::UserRole + 3;

private slots:
	void startSearch();
	void deleteImage();
	void select();
	void viewImage();
	void imageClicked(const QModelIndex& index);
	void pageNumberChanged(const QString& newstring);
};


#endif
